// 引入 lodash 库，用于数据处理和操作
const _ = require('lodash')
// 引入 path 模块，用于处理文件和目录路径
const path = require('path')
// 获取项目根目录
const ROOT_DIR = path.join(__dirname, '..')
// helpers 目录
const HELPERS_DIR = path.join(ROOT_DIR, 'helpers')

const { padArrayNumbers } = require(path.join(HELPERS_DIR, 'utils.js'))

// 过滤并排序的核心函数
const filterAndSortGroups = (groups, forecastConfig) => {
  const groupTypes = forecastConfig.map((config) => config.group_type)

  // 先过滤符合条件的组
  const filteredGroups = groups.filter((group) => {
    const currentCfg = forecastConfig.find((config) => config.group_type === group.groupType)
    return groupTypes.includes(group.groupType) && group.stats.accuracyRate >= currentCfg.minAaccuracyRate
  })

  // 按group_type分组
  const groupedByType = _.groupBy(filteredGroups, 'groupType')

  // 获取每个group_type的配置count
  const typeConfigs = _.keyBy(forecastConfig, 'group_type')

  // 对每个group_type组内按accuracyRate排序并限制数量
  const sortedGroups = Object.entries(groupedByType).flatMap(([type, groups]) => {
    return groups.sort((a, b) => b.stats.accuracyRate - a.stats.accuracyRate).slice(0, typeConfigs[type].count)
  })

  // 最后按forecastConfig中的group_type顺序排序
  return sortedGroups.sort((a, b) => {
    const typeA = groupTypes.indexOf(a.groupType)
    const typeB = groupTypes.indexOf(b.groupType)
    return typeA - typeB
  })
}

const getLuckAndForecastGroupNames = (
  redContinuousLimit2Group,
  greenReboundRedNonContinuousGroup,
  greenContinuousGroup,
  currentHistoryItem,
  numberGroupMap,
  flatGroupMap
) => {
  const a = redContinuousLimit2Group.map((group) => ({
    ...group,
    forecast_type: 'red_continuous_limit_2'
  }))
  const b = greenReboundRedNonContinuousGroup.map((group) => ({
    ...group,
    forecast_type: 'green_rebound_red_non_continuous'
  }))
  const c = greenContinuousGroup.map((group) => ({
    ...group,
    forecast_type: 'green_continuous'
  }))

  const groupNamesMap = {
    red_continuous_limit_2: [],
    green_rebound_red_non_continuous: [],
    green_continuous: []
  }

  // 对每个分组执行过滤和排序操作
  const a_config = [
    // { group_type: 'two', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'three', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'four', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'five', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'six', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'seven', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'eight', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'ten', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'twelve', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'sixteen', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'twentyfour', count: 1, minAaccuracyRate: 0.8 }

    // { group_type: 'twentyfour', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'sixteen', count: 1, minAaccuracyRate: 0.8 },
    // { group_type: 'twelve', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'ten', count: 5, minAaccuracyRate: 0.9 },
    { group_type: 'eight', count: 2, minAaccuracyRate: 0.9 },
    { group_type: 'seven', count: 2, minAaccuracyRate: 0.9 },
    { group_type: 'six', count: 2, minAaccuracyRate: 0.9 },
    { group_type: 'five', count: 2, minAaccuracyRate: 0.9 },
    { group_type: 'four', count: 3, minAaccuracyRate: 0.9 },
    { group_type: 'three', count: 3, minAaccuracyRate: 0.9 },
    { group_type: 'two', count: 3, minAaccuracyRate: 0.9 },
    { group_type: 'twelve', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'sixteen', count: 1, minAaccuracyRate: 0.8 }
  ]
  const filteredSortedA = filterAndSortGroups(a, a_config)

  // console.log('filteredSortedA', filteredSortedA)

  const b_config = [
    { group_type: 'two', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'three', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'four', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'five', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'six', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'seven', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'eight', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'ten', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'twelve', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'sixteen', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'twentyfour', count: 1, minAaccuracyRate: 0.8 }
  ]
  const filteredSortedB = filterAndSortGroups(b, b_config)

  const c_config = [
    { group_type: 'two', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'three', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'four', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'five', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'six', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'seven', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'eight', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'ten', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'twelve', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'sixteen', count: 1, minAaccuracyRate: 0.8 },
    { group_type: 'twentyfour', count: 1, minAaccuracyRate: 0.8 }
  ]
  const filteredSortedC = filterAndSortGroups(c, c_config)

  // 最终合并结果
  // const allGroups = [...filteredSortedA, ...filteredSortedB, ...filteredSortedC]
  const allGroups = [...filteredSortedA]

  // 用 Set 来避免重复的号码
  const luckySet = new Set()

  allGroups.forEach((group) => {
    const { groupName, forecast_type } = group
    const index = numberGroupMap[currentHistoryItem.number][groupName]
    const sub_array = flatGroupMap[groupName][index]

    if (luckySet.size < 32) {
      // 一个号码一个号码去添加
      sub_array.forEach((number) => {
        if (luckySet.size < 32) {
          luckySet.add(number)
        }
      })
      groupNamesMap[forecast_type].push(groupName)
    }
  })

  if (luckySet.size < 32) {
    console.log('luckySet.size', luckySet.size)
  }

  const uniqueLuckyArray = [...luckySet]

  // 生成 1-49 的数组
  const all_numbers = Array.from({ length: 49 }, (_, i) => i + 1).map((num) => (num < 10 ? `0${num}` : `${num}`))

  // 幸运号码
  const lucky_array = uniqueLuckyArray.map((item) => item.toString().padStart(2, '0'))

  // 杀号
  const kill_array = all_numbers.filter(
    (item) => !lucky_array.map((item) => item.toString().padStart(2, '0')).includes(item.toString().padStart(2, '0'))
  )

  return {
    lucky_array,
    kill_array,
    red_continuous_limit_2: groupNamesMap.red_continuous_limit_2,
    green_rebound_red_non_continuous: groupNamesMap.green_rebound_red_non_continuous,
    green_continuous: groupNamesMap.green_continuous
  }
}

module.exports = { getLuckAndForecastGroupNames }
